/** 
* Copyright (C) 2012-2017 ZEEI Inc.All Rights Reserved.
* 项目名称：common
* 文件名称：PropertiesUtil.java
* 包  名 称：com.zeei.common.utils
* 文件描述：配置文件工具类
* 创建日期：2017年8月3日上午9:39:11
* 
* 修改历史
* 1.0 quanhongsheng 2017年8月3日上午9:39:11 创建文件
*
*/
package com.zeei.ems.base.util;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


/**
 * @类型名称：PropertiesUtil
 * @类型描述：配置文件工具类
 * @功能描述：提供配置文件操作方法
 * @创建作者：quanhongsheng
 */
public class PropertiesUtil {

	private final static Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);

	/** 默认的文件名称 */
	private static final String DEFAULT_FILE_NAME = "config.properties";

	// 定义配置文件对象
	private static Properties props = null;
	
	static{
		loadProperties(StringUtil.empty());
	}

	/**
	 * 返回默认配置文件中指定key对应的值
	 *
	 * @return
	 */
	public static Properties getProperty() {
		return props;
	}

	/**
	 * @param configFile
	 *            /db.properties"
	 * @return
	 */
	private static Properties loadProperties(String configFile) {
		String fileName = DEFAULT_FILE_NAME;
		if (!StringUtils.isEmpty(configFile)) {
			fileName = configFile;
		}
		if (!fileName.startsWith("/")) {
			fileName = "/" + fileName;
		}
		Resource resource = new ClassPathResource(fileName);
		try {
			props = PropertiesLoaderUtils.loadProperties(new EncodedResource(resource,"UTF-8"));
		} catch (IOException e) {
			LOGGER.error("read config file failed", e);
		}
		return props;
	}

	/**
	 * 返回默认配置文件中指定key对应的值
	 * 
	 * @param propertyName
	 * @return
	 */
	public static String getProperty(String propertyName) {
		return getProperty(null, propertyName);
	}

	/**
	 * 返回配置文件中指定key对应的值
	 * 
	 * @param properties
	 * @param propertyName
	 * @return
	 */
	public static String getProperty(Properties properties, String propertyName) {
		if (properties == null) {
			properties = props;
		}
		if (properties != null) {
			return properties.getProperty(propertyName);
		}
		return null;
	}

	/**
	 * 返回key的集合
	 * 
	 * @param properties
	 * @return
	 */
	public static Set<String> getPropertyNames(Properties properties) {
		return properties.stringPropertyNames();
	}

	/**
	 * 
	 * getValue:获取某个配置项的值
	 *
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		return props.getProperty(key);
	}

	/**
	 * 
	 * iteratorProps:遍历某个配置项的所有属性
	 *
	 * @return
	 * @throws IOException
	 *             String
	 */
	public Map<String, String> iteratorProps() throws IOException {
		Map<String, String> kv = new HashMap<String, String>();

		@SuppressWarnings("rawtypes")
		Iterator it = props.entrySet().iterator();
		while (it.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			kv.put(key, value);
		}
		return kv;
	}

	public static Map<String, String> readProperties() {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<?> enum1 = props.propertyNames();
		while (enum1.hasMoreElements()) {
			String strKey = (String) enum1.nextElement();
			String strValue = props.getProperty(strKey);

			map.put(strKey, strValue);
		}
		
		return map;
	}

	public static Map<String, String> readPropertiesByFile(String configFile) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			String fileName = configFile;
			if (!fileName.startsWith("/")) {
				fileName = "/" + fileName;
			}
			Resource resource = new ClassPathResource(fileName);
			Properties props = PropertiesLoaderUtils.loadProperties(new EncodedResource(resource,"UTF-8"));
			Enumeration<?> enum1 = props.propertyNames();
			while (enum1.hasMoreElements()) {
				String strKey = (String) enum1.nextElement();
				String strValue = props.getProperty(strKey);

				map.put(strKey, strValue);
			}
		} catch (IOException e) {
			LOGGER.error("read config file failed", e);
		}
		
		return map;
	}
}
